package com.hyc.calenderdemo;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.Calendar;

public class CalenderDemoActivity extends AppCompatActivity implements View.OnClickListener {


    private Button mDeleteEventButton;
    private Button mWriteEventButton;
    final int callbackId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calender_demo);
        checkPermission(callbackId, Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR);
        setupViews();
    }

    private void checkPermission(int callbackId, String... permissionsId) {
        boolean permissions = true;
        for (String p : permissionsId) {
            permissions = permissions && ContextCompat.checkSelfPermission(this, p) == PERMISSION_GRANTED;
        }

        if (!permissions)
            ActivityCompat.requestPermissions(this, permissionsId, callbackId);
    }

    private void setupViews() {
        mDeleteEventButton = (Button) findViewById(R.id.deleteEventButton);
        mWriteEventButton = (Button) findViewById(R.id.writeEventButton);
        mDeleteEventButton.setOnClickListener(this);
        mWriteEventButton.setOnClickListener(this);
    }

    /**
     * 检查是否存在现有账户，存在则返回账户id，否则返回-1
     */

    /**
     * 添加日历账户，账户创建成功则返回账户id，否则返回-1
     */


    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        Long eventId = Long.parseLong(String.valueOf((int)Math.random()*999+1));
        if (v == mWriteEventButton) {
            Calendar mCalendar = Calendar.getInstance();
            mCalendar.setTimeInMillis(System.currentTimeMillis() + 1 * 60 * 1000);
            long start = mCalendar.getTime().getTime();
            mCalendar.setTimeInMillis(start + 1 * 60 * 1000);
            long end = mCalendar.getTime().getTime();
            CalendarReminderUtils.addCalendarEvent(this, eventId,"工作提醒", "该休息啦", start, end, 0,true);
            Log.d("============eventid", "onClick: " + eventId);
        } else if (v == mDeleteEventButton) {
//            CalendarReminderUtils.queryCalendarData(CalendarContract.Calendars.CONTENT_URI, this);
            CalendarReminderUtils.deleteCalendarEvent(this, eventId);
        }
    }


    /*
     * String message, int hour, int minutes, int resId
     * 暂时使用默认id
     * */
    private void createAlarm(String message, int hour, int minutes) {
        // 设置提醒的日期和时间
        //action为AlarmClock.ACTION_SET_ALARM
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                //闹钟的小时
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                //闹钟的分钟
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes)
                //响铃时提示的信息
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                //用于指定该闹铃触发时是否振动
                .putExtra(AlarmClock.EXTRA_VIBRATE, true)
                //一个 content: URI，用于指定闹铃使用的铃声，也可指定 VALUE_RINGTONE_SILENT 以不使用铃声。
                //如需使用默认铃声，则无需指定此 extra。
//                .putExtra(AlarmClock.EXTRA_RINGTONE, ringtoneUri)
                //一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
                // 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
                //对于一次性闹铃，无需指定此 extra
//                .putExtra(AlarmClock.EXTRA_DAYS, calendar.get(Calendar.DAY_OF_WEEK) - 1)
                //如果为true，则调用startActivity()不会进入手机的闹钟设置界面
                .putExtra(AlarmClock.EXTRA_DAYS, 1687968000)
                .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        if (intent.resolveActivity(getPackageManager()) != null) {
            Toast.makeText(CalenderDemoActivity.this, "设置闹钟成功!!!", Toast.LENGTH_LONG).show();
            startActivity(intent);
        }
    }
}